<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/16
 * Time: 9:54
 */

namespace Admin\Controller;

use Think\Page;

/*
 * 管理组类
 *  */

class GroupController extends AdminController
{
    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = M('group');
    }

    /*
     * 管理组展示
     * */
    public function index()
    {
        //搜索
        $g_name = I('get.g_name', '', 'trim');
        if ($g_name) {
            $where['g_name'] = $g_name;
        }
        $create_time = strtotime(I('get.create_time'));
        if ($create_time) {
            $where['create_time'] = array('egt', $create_time);
        }
        $count = $this->model->where($where)->count();
        $page = new Page($count, 20);
        $groupdata = $this->model->where($where)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $string = "";
        foreach ($groupdata as &$value) {
            $g_member = unserialize($value['g_member']);
            unset($value['g_member']);
            foreach ($g_member as $val) {
                $name = M('follow')->find($val);
                $value['g_member'] .= $name['nickname'] . ',';
            }
            $value['g_member'] = trim($value['g_member'], ',');
        }
//        var_dump($groupdata);
        $this->assign('groupdata', $groupdata);
        $this->assign('page', $page);
        $this->display();
    }

    /*
     * 组成员添加控制器
     * */
    public function add()
    {
        if (IS_POST) {
            //接收组名称
            $g_name = I('post.g_name', '', 'trim');
            if (empty($g_name)) {
                $this->error('对不起,组名称不能为空');
            }
            $data['g_name'] = $g_name;
            //接收组账号
            $g_account = I('post.g_account', '', 'trim');
            if (empty($g_account)) {
                $this->error('对不起,组账号不能为空');
            }
            if ($this->model->where(array('g_account' => $g_account))->find()) {
                $this->error('对不起,该组账号已存在');
            }
            $data['g_account'] = $g_account;
            //接收组密码
            $g_password = I('post.g_password', '', 'trim');
            if (empty($g_password)) {
                $this->error('对不起,组密码不能为空');
            }
            $data['g_password'] = md5($g_password);
            //接收组成员
            $g_member = I('post.g_member', '', 'trim');
            if (!empty($g_member)) {
                $memberarr = explode(',', $g_member);
                foreach($memberarr as $val){
                    $where['_string']="g_member regexp ':\"{$val}\";'";
                    $search=$this->model->where($where)->find();
                    if($search){
                        $this->error('对不起,ID为'.$val.'的成员,已在其他组中');
                    }
                }
                $data['g_member'] = serialize($memberarr);
            }
            $data['create_time'] = time();
            $res = $this->model->add($data);
            if ($res) {
                $this->success('恭喜你,添加成功', U('group/index'));
            } else {
                $this->error('对不起,添加失败');
            }
        }
        if(IS_AJAX){
          $sql['is_agent'] = 1;
          $sql['maker_id'] = array('eq'=>1);
          $nickname = $_GET['nickname'];
          if(!empty($nickname)){
            $sql['nickname'] = array('like','%'.trim($nickname).'%');
          }
          $mobile = $_GET['mobile'];
          if(!empty($mobile)){
            $sql['mobile'] = array('like','%'.trim($mobile).'%');
          }
          $data = M('follow')->where($sql)->select();
          $this->ajaxReturn($data);
        }
        //查询经纪人
        $list = M('follow')->where('is_agent = 1 and maker_id != 1')->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function edit()
    {
        if (IS_POST) {
            //接收组名称
            $id = I('post.id');
            $g_name = I('post.g_name', '', 'trim');
            if (empty($g_name)) {
                $this->error('对不起,组名称不能为空');
            }
            $data['g_name'] = $g_name;
            //接收组账号
            $g_account = I('post.g_account', '', 'trim');
            if (empty($g_account)) {
                $this->error('对不起,组账号不能为空');
            }
            $yandata=$this->model->where(array('g_account' => $g_account))->find();
            if ($yandata && $yandata['id']!=$id) {
                $this->error('对不起,该组账号已存在');
            }
            $data['g_account'] = $g_account;
            //接收组密码
            $g_password = I('post.g_password', '', 'trim');
            if (!empty($g_password)) {
                $data['g_password'] = md5($g_password);
            }

            //接收组成员
            $g_member = I('post.g_member', '', 'trim');
            $g_member = trim($g_member,',');
            $memberarr = explode(',', $g_member);
            foreach($memberarr as $val){
                $where['_string']="g_member regexp ':\"{$val}\";'";
                $search=$this->model->where($where)->find();
                if($search && $search['id']!=$id){
                    $this->error('对不起,ID为'.$val.'的成员,已在其他组中');
                }
            }

            $data['g_member'] = serialize($memberarr);

            $data['create_time'] = time();
            $res = $this->model->where(array('id' => $id))->save($data);
            if ($res !== false) {
                $this->success('恭喜你,修改成功', U('group/index'));
            } else {
                $this->error('对不起,修改失败');
            }
        }
        $id = I('get.id');
        $showdata = $this->model->find($id);
        $showdata['g_member'] = implode(unserialize($showdata['g_member']), ',');
        $this->assign('showdata', $showdata);
        $this->display();
    }

    /*
     * 删除组
     * */
    public function del_group()
    {
        $id = I('post.ids');
        foreach ($id as $val) {
            $search = $this->model->find($val);
            if (!empty($search['g_member'])) {
                $this->error('对不起,ID为' . $val . '的组下面有成员,不能删除');
            }
        }
        $page = I('get.p');
        $where['id'] = array('in', $id);
        $res = $this->model->where($where)->delete();
        if ($res) {
            $this->success('恭喜你,删除成功', U('Group/index', array('p' => $page)));
        } else {
            $this->error('对不起,删除失败');
        }
    }

}
